package com.yc.home.cloudother.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.yc.bean.Result;
import com.yc.entity.Paper;
import com.yc.entity.PaperDetail;
import com.yc.home.cloudother.mapper.PaperDetailMapper;
import com.yc.home.cloudother.mapper.PaperMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzk
 * @since 2024-10-12
 */
@RestController
@RequestMapping("/paper")
public class PaperController {

    @Autowired
    PaperMapper paperMapper;

    @Autowired
    PaperDetailMapper paperDetailMapper;

    @Transactional
    @RequestMapping("add")
    public Result add(@Valid @RequestBody Paper paper, Errors errors){

        //自定义判断
        if (paper.getTime() == 0) {
            //rejectValue()   对字段的验证        reject()    对整个对象的验证
            errors.rejectValue("time", "is_empty", "考试时间不能为0！");
        }

        Timestamp begin = paper.getBegin();
        Timestamp end = paper.getEnd();

        if (begin != null && end != null && begin.after(end)) {
            errors.rejectValue("begin", "error.begin.after.end", "开始时间不能大于结束时间！");
        }

        //字段验证
        if(errors.hasErrors()){
            return new Result(0, "信息错误！", errors.getAllErrors());
        }

        paper.setStatus(1);

        paperMapper.insert(paper);

        for (PaperDetail detail : paper.getDetails()) {
            detail.setPid(paper.getId());
            paperDetailMapper.insert(detail);
        }
        return new Result(1, "加入成功", null);
    }

    @RequestMapping("query")
    IPage query(@RequestParam(defaultValue = "1") int current,
                       @RequestParam(defaultValue = "5") int size, @RequestParam String search, @RequestParam String uid){
        LambdaQueryWrapper<Paper> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Paper::getCreateUid, uid);
        lqw.eq(Paper::getStatus, 1);
        if(search!=null && search.isBlank() == false){
            lqw.like(Paper::getTitle, search);
        }
        Page<Paper> p = new PageDTO<>(current, size);
        return paperMapper.selectPage(p, lqw);
    }

    @RequestMapping("del")
    Result del(String id){
        Paper paper = paperMapper.selectById(id);
        paper.setStatus(0);
        paperMapper.updateById(paper);
        return new Result(1, "删除成功！", null);
    }

    //查询考卷
    @RequestMapping("get")
    public Paper findById(int id){
        return paperMapper.findByid(id);
    }

    @RequestMapping("getPaper")
    Result getPaper(String uid){
        LambdaQueryWrapper<Paper> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Paper::getCreateUid, uid);
        List<Paper> papers = paperMapper.selectList(lqw);
        return new Result(1, "试卷基本信息", papers);
    }

    @RequestMapping("untime")
    Result untime(String id){
        Paper paper = paperMapper.selectById(id);
        Timestamp begin = paper.getBegin();
        Timestamp end = paper.getEnd();
        return isCurrentTimeInRange(begin, end);
    }

    public Result isCurrentTimeInRange(Timestamp begin, Timestamp end){
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        if(timestamp.before(begin)){
            return new Result(0, "参考时间未到！", null);
        }
        if(timestamp.after(end)){
            return new Result(0, "参考时间已过！", null);
        }
        return new Result(1, "成功", null);
    }

    //获取剩余时间
    @RequestMapping("remainingTime")
    Result remainingTime(String pid){
        Paper paper = paperMapper.selectById(pid);
        Timestamp end = paper.getEnd();
        //获取当前时间
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        long diffInMillis = end.getTime() - timestamp.getTime();
        long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(diffInMillis);
        if(diffInMinutes > paper.getTime()){
            return new Result(1, "完整时间", paper.getTime());
        }else{
            return new Result(0, "剩余时间", diffInMinutes);
        }
    }

    @RequestMapping("getone")
    Result getone(String pid){
        Paper paper = paperMapper.selectById(pid);
        return new Result(1, "试卷", paper);
    }

}

